var intTextBox=0;
//function to add phone number
var addPhoneNumber = function(){
	
	intTextBox = intTextBox + 1;
	var divPhoneNumber = 'divPhoneNumber'+ intTextBox;
	var contentID = document.getElementById("phone-col");
	var newTBDiv = document.createElement("div");
	newTBDiv.setAttribute("id","strTextPn"+intTextBox);
	newTBDiv.innerHTML = "<br /><input type='text' id='" + intTextBox + "' name='phone[]'><a href='javascript:void(0);' onclick='removePhoneNumber("+intTextBox+")'  name='mobileNumberDelete'>&nbsp;&nbsp;[-]</a><br /><div id='" + divPhoneNumber + "' />";
	contentID.appendChild(newTBDiv);
	
};
//function to remove Phone number
var removePhoneNumber = function(intText)
{
	
	if(intText != 0)
	{
		var contentID = document.getElementById("phone-col");
		contentID.removeChild(document.getElementById("strTextPn"+intText));
		
	}
};
//function to add offer details
var addOfferDetails = function(){
	
	intTextBox = intTextBox + 1;
	var divOfferDetails = 'divOfferDetails'+ intTextBox;
	var contentID = document.getElementById("offerdetails-col");
	var newTBDiv = document.createElement("div");
	newTBDiv.setAttribute("id","strTextOff"+intTextBox);
	newTBDiv.innerHTML = "<br /><textarea rows='3' cols='20' id='" + intTextBox + "' name='offer_details[]'></textarea><a href='javascript:void(0);' onclick='removeOfferDetails("+intTextBox+")'  name='offerDetailsDelete'>&nbsp;&nbsp;[-]</a><br /><div id='" + divOfferDetails + "' />";
	contentID.appendChild(newTBDiv);
	
};
//function to remove offer details
var removeOfferDetails = function(intText)
{
	
	if(intText != 0)
	{
		var contentID = document.getElementById("offerdetails-col");
		contentID.removeChild(document.getElementById("strTextOff"+intText));
		
	}
};
//function to do validations for restaurant form
function CheckAll()
{
	
	ClearDiv();
	var counter = 0;
	
	 //get all div objects
	var objDivUsername 			    = document.getElementById('usernameDiv');
	var objDivPassword 			    = document.getElementById('passwordDiv');
	var objDivConfirmPwd 			= document.getElementById('confirmPwdDiv');
	var objDivRestaurant 			= document.getElementById('restaurantNameDiv');
    var objDivAddress				= document.getElementById('addressDiv');
    var objDivPhoneNumber			= document.getElementById('phoneDiv');
    //var objDivLatitude				= document.getElementById('latDiv');
    //var objDivLongitude				= document.getElementById('longDiv');
    var objDivArea					= document.getElementById('areaDiv');
    var objDivContactPerson			= document.getElementById('contactPersonDiv');
   
    var objUsername  				= document.getElementById('userName').value;
    var objPassword  				= document.getElementById('pwd').value;
    var objConfirmPwd  				= document.getElementById('confirm_pwd').value;
    var objRestaurantName  			= document.getElementById('restaurantName').value;
    var objAddress					= document.getElementById('address').value;
    //var objLat						= document.getElementById('latitude').value;
    //var objLong						= document.getElementById('longitude').value;
    var objContactPerson			= document.getElementById('contactPerson').value;
    var objArea						= document.getElementById('area').value;
    
    if (objUsername == "")
    {
    	
    	objDivUsername.innerHTML = "<span class='error'>Please Enter Username</span>";
        counter=counter+1;
    }
    if (objUsername != "")
    {
    	
    	if(!isValidEmail(document.getElementById('userName')))
	    {
    		objDivUsername.innerHTML = "<span class='error'>Please Enter Valid Email</span>";
	        counter=counter+1;
	    }
    	
    }
    if (objPassword == "")
    {
    	
    	objDivPassword.innerHTML = "<span class='error'>Please Enter Password</span>";
        counter=counter+1;
    }
    if (objConfirmPwd == "")
    {
    	
    	objDivConfirmPwd.innerHTML = "<span class='error'>Please Enter Confirm Password</span>";
        counter=counter+1;
    }
    if( objPassword != objConfirmPwd) 
    {
    	
    	objDivPassword.innerHTML = "<span class='error'>Both passwords should be same.</span>";
    	objDivConfirmPwd.innerHTML = "";
    	 counter=counter+1;
    }
    if (objArea == "")
    {
    	
    	objDivArea.innerHTML = "<span class='error'>Please Enter Area</span>";
        counter=counter+1;
    }
    if (objRestaurantName=="")
    {
    	
    	objDivRestaurant.innerHTML = "<span class='error'>Please Enter Restaurant Name</span>";
        counter=counter+1;
       
    }
    if (objAddress=="")
    {
    	
    	objDivAddress.innerHTML = "<span class='error'>Please Enter Address</span>";
        counter=counter+1;
       
    }
    /* 
  //get the latitude object
    var objGlat = document.getElementById('latitude');
    
    //Validate latitude 
    if (objGlat.value == "")
    {
    	objDivLatitude.innerHTML = "<font color='red'>Please enter Latitude value </font>";
        counter=counter+1;
        
    }
    else if(!IsNumerics(objGlat.value))
    {
    	objDivLatitude.innerHTML = "<font color='red'>Please enter Numbers only</font>";;
        counter=counter+1;
            
     }
    else
    {
	   objDivLatitude.innerHTML = "";
    }
    
    
  //get the longitude object
    var objGlong = document.getElementById('longitude');
    
    //Validate longitude 
    if (objGlong.value == "")
    {
    	objDivLongitude.innerHTML = "<font color='red'>Please enter Longitude value </font>";
        counter=counter+1;
        
    }
    else if (!IsNumerics(objGlong.value))
    {
        	objDivLongitude.innerHTML = "<font color='red'>Please enter Numbers only</font>";;
            counter=counter+1;
            
    }
    else
    {
    	objDivLongitude.innerHTML = "";
    }
    
      */
   
    var mobile = document.getElementsByName('phone[]');
    
    // Check for phone number
	for (var i=0; i < mobile.length; i++) 
	{
		
			
			if(mobile[i].value == "")
			{
				if(i==0)
				{
					
					document.getElementById('phoneDiv').innerHTML = "<span class='error'>Please Enter Phone Number</span>";
					counter=counter+1;
				}
				else
				{
					
					document.getElementById('phoneDiv'+i).innerHTML = "<span class='error'>Please Enter Valid Number</span>";
					counter=counter+1;
				}
			}
	}
    if (objContactPerson=="")
    {
    	
    	objDivContactPerson.innerHTML = "<span class='error'>Please Enter Contact Person Name</span>";
        counter=counter+1;
       
    }
    if (objContactPerson!="")
    {
    	
    	if(!IsAlphabets(objContactPerson))
    	{
	    	objDivContactPerson.innerHTML = "<span class='error'>Please Enter Alphabets Only</span>";
	        counter=counter+1;
    	}
       
    }
    //Checking condition whether counter is  zero   
    if(counter >0)
    {
    	
    	document.getElementById('errorMsg').innerHTML = "<span class='error'>Please fill in the Required Fields.</span>";
    	return false;
    }
    else
    {
    	
    	document.getElementById('errorMsg').innerHTML = "<span class='error'>Data Saved Succesfully.</span>";
    	return true;
    }
}
//This function is used to check whether latitude and longitude values are valid
function latLonVal(str)
{
	var regExp =/^[0-9]{2}\.[0-9]{5,6}$/;
	if(str != null && str != "")
    {
       var val = regExp.test(str);
        if ( val == false)
        {
          return false;
        }
      
    }
    else
    {
      return false;
    }
    return true;
}

//clearing offer divs
function clearOfferDivs()
{
	document.getElementById('captionDiv').innerHTML  = "";
	document.getElementById('divOfferDetails').innerHTML = "";
	document.getElementById('startDateDiv').innerHTML = "";
	document.getElementById('endDateDiv').innerHTML = "";
}



//function to do validations for offer form
function checkOfferForm()
{
	
	
	//clearing offer divs
	clearOfferDivs();
	
	var counter = 0;
	
		 //get all div objects
	  var objDivCaption 			    = document.getElementById('captionDiv');
	  var objDivOfferDetails 			= document.getElementById('divOfferDetails');
	  var objDivStartDate 				= document.getElementById('startDateDiv');
	  var objDivEndDate 				= document.getElementById('endDateDiv');
  
	   var objCaption  					= document.getElementById('caption').value;
	   var objstartDate  				= document.getElementById('start_date').value;
	   var objEndDate  					= document.getElementById('end_date').value;
	   
	   if (objCaption=="")
	    {
	    	
		   objDivCaption.innerHTML = "<span class='error'>Please Enter Caption</span>";
	        counter=counter+1;
	       
	    }
	   if (objstartDate=="")
	    {
	    	
		   objDivStartDate.innerHTML = "<span class='error'>Please Enter Start Date</span>";
	        counter=counter+1;
	       
	    }
	   if (objEndDate=="")
	    {
	    	
		   objDivEndDate.innerHTML = "<span class='error'>Please Enter End Date</span>";
	        counter=counter+1;
	       
	    }
	   var offerDetails = document.getElementsByName('offer_details[]');
	    
	    // Check for phone number
		for (var i=0; i < offerDetails.length; i++) 
		{
				if(offerDetails[i].value == "")
				{
					if(i==0)
					{
						document.getElementById('divOfferDetails').innerHTML  = "<span class='error'>Please enter Offer Details</span>";
						counter=counter+1;
					}
					else
					{
						document.getElementById('divOfferDetails'+i).innerHTML = "<span class='error'>Please enter Offer Details</span>";
						counter=counter+1;
					}
					
					
				}
				
		}
	   
	 //Checking condition whether counter is  zero   
	    if(counter >0)
	    {
	    	document.getElementById('errorMsg').innerHTML = "<span class='error'>Please fill in the Required Fields.</span>";
	    	return false;
	    }
	    else
	    {
	    	document.getElementById('errorMsg').innerHTML = "<span class='error'>Data Saved Succesfully.</span>";
	    	return true;
	    }
  
}

//function is used to check vaild email address
function isValidEmail(str) {


    var at = "@";
    var dot = ".";

    str = str.value;
    var lat = str.indexOf(at);
    var lstr = str.length;
    var ldot = str.indexOf(dot);
    if (str.indexOf(at) == -1) {
        return false;
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        return false;
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        return false;
    }

    if (str.indexOf(at, (lat + 1)) != -1) {
        return false;
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        return false;
    }

    if (str.indexOf(dot, (lat + 2)) == -1) {
        return false;
    }

    if (str.indexOf(" ") != -1) {
        return false;
    }

    return true;
}

//function is used to check the string is Alphabers
function IsAlphabets(str) {
    var tempString="";
    var regExp = /^[A-Z a-z]$/;
    if(str != null && str != "")
    {
      for(var i = 0; i < str.length; i++)
      { 
        if (!str.charAt(i).match(regExp))
        {
          return false;
        }
      }
    }
    else
    {
      return false;
    }
    return true;
}

//function is used to check the string is Numeric

function IsNumeric(sText) {
    var ValidChars = "0123456789";
    var IsNumber = true;
    var Char;


    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;

}

//function is used to check the string is Numeric
function IsNumerics(sText) {
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;


    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;

}
//function is used clear all divs
function ClearDiv()
{
	document.getElementById('usernameDiv').innerHTML = "";
	document.getElementById('passwordDiv').innerHTML = "";
	document.getElementById('areaDiv').innerHTML = "";
    document.getElementById('restaurantNameDiv').innerHTML = "";
    document.getElementById('addressDiv').innerHTML = "";
    document.getElementById('phoneDiv').innerHTML = "";
    document.getElementById('latDiv').innerHTML = "";
    document.getElementById('longDiv').innerHTML = "";
    document.getElementById('contactPersonDiv').innerHTML = "";
         
}
//code to check  duplicate of username in restaurant
function chkUserName(userName)
{
	
	var dataString = "userName="+userName;
	$.ajax({		
	   type: "Get",
	   url: userNameUrl,
	   data: dataString,
	    success: function(html)
	    {
			
			document.getElementById('usernameDiv').innerHTML = "<span class='error'>"+html+"</span>";
			
	    }
});
}
//function to validate registration form
var validateRegistration = function(){
	
	
	//function to clear team divs
	clearRegisterDivs();
	
	var email 		= document.getElementById('userName').value;
	var pwd 		= document.getElementById('reg_password').value;
	var cpwd 		= document.getElementById('cpassword').value;
	var screenName  = document.getElementById('screen_name').value;
	var fileName	= document.getElementById('file_name').value;
		
	var objDivEmail 			    = document.getElementById('emailDiv');
	var objDivPassword 			    = document.getElementById('passwordDiv');
	var objDivConfirmPwd 			= document.getElementById('cpasswordDiv');
	var objDivScreen				= document.getElementById('screenDiv');
    var objDivTerms					= document.getElementById('termsDiv');
    var objDivFile					= document.getElementById('fileNameDiv');
	
	var counter = 0;
	
	
	if (email == "")
    {
		
		objDivEmail.innerHTML = "<span class='error'>Please Enter Your Email Address.</span>";
        counter=counter+1;
    }
    if (email != "")
    {
    	
    	if(!isValidEmail(document.getElementById('userName')))
	    {
    		
    		objDivEmail.innerHTML = "<span class='error'>Please Enter Valid Email</span>";
	        counter=counter+1;
	    }
    	
    }
    if (pwd == "")
    {
    	
    	objDivPassword.innerHTML = "<span class='error'>Please Enter Password</span>";
        counter=counter+1;
    }
    if (cpwd == "")
    {
    	
    	objDivConfirmPwd.innerHTML = "<span class='error'>Please Enter Confirm Password</span>";
        counter=counter+1;
    }
    if( pwd != cpwd) 
    {
    	
    	objDivPassword.innerHTML = "<span class='error'>Both passwords should be same.</span>";
    	objDivConfirmPwd.innerHTML = "";
    	 counter=counter+1;
    }
	if(screenName == "")
	{
		objDivScreen.innerHTML =  "<span class='error'>Please Enter Screen Name.</span>";
		counter = counter + 1;
	}
/*	if(fileName == "")
	{
		objDivFile.innerHTML =  "<span class='error'>Please upload your image.</span>";
		counter = counter + 1;
	}
*/	
	//validation for terms
   
	$("input[id^='terms']").each( function ()
	{
		if(this.checked == false )
    	{
			objDivTerms.innerHTML = "<font color='red'>Please check the terms and conditions.</font>";
			counter=counter+1;
    	}
		
		
	});
	
	
	if(counter > 0)
	{
		return false;
	}
	else
	{
		return true;
	}
};

var clearRegisterDivs = function()
{
	document.getElementById('usernameDiv').innerHTML = "";
	document.getElementById('passwordDiv').innerHTML = "";
	document.getElementById('cpasswordDiv').innerHTML = "";
	document.getElementById('screenDiv').innerHTML = "";
    document.getElementById('termsDiv').innerHTML = "";
    document.getElementById('fileNameDiv').innerHTML = "";
};